package com.zhiyou.shop.controller;

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhiyou.shop.model.UserModel;
import com.zhiyou.shop.service.IUserService;

@Controller
@RequestMapping("/front")
public class RegisterController {
      @Autowired
      IUserService userService;
	
	@RequestMapping("register.do")
	public String register(){
		
		
		return "front/register";
	}
	
	@ResponseBody
	@RequestMapping(value="emailRegister.do",method=RequestMethod.POST)
	public Object emailRe(UserModel user){
		System.out.println("2222");
		HashMap map = new HashMap<>();
		boolean result = userService.canRegister(user);
		if (result) {
			if (user.getLoginPsw().equals(user.getLoginPsw2())) {
				userService.addRegister(user);
				map.put("success", true);
				map.put("message", "注册成功，快去登录吧！");
			} else {
				map.put("success", false);
				map.put("message", "两次输入密码不一致，注册失败！");
			}
		} else{
			map.put("success", false);
			map.put("message", "账号已存在，注册失败！");
		}
		return map;
	}
	
	@ResponseBody
	@RequestMapping(value="phoneRegister.do",method=RequestMethod.POST)
	public Object phoneRe(UserModel user){
		HashMap map = new HashMap<>();
		boolean result = userService.canRegister(user);
		if (result) {
			if (user.getLoginPsw().equals(user.getLoginPsw2())) {
				userService.addRegister(user);
				map.put("success", true);
				map.put("message", "注册成功，快去登录吧！");
			} else {
				map.put("success", false);
				map.put("message", "两次输入密码不一致，注册失败！");
			}
		} else{
			map.put("success", false);
			map.put("message", "账号已存在，注册失败！");
		}
		return map;
	}
}
